package com.ganbo.stock.service.impl;

import com.ganbo.stock.entity.BigDeal;
import com.ganbo.stock.entity.Stock;
import com.ganbo.stock.enums.ServiceType;
import com.ganbo.stock.utils.ThreadUtils;
import com.ganbo.stock.repository.BigDealRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 当天逐笔大单交易
 *
 * @Author: ganbo
 * @Date: 2020/12/28 18:07
 */

@Service
@Slf4j
public class BigDealService extends BaseService {

    @Autowired
    BigDealRepository bigDealRepository;
    @Autowired
    StockService stockService;
  ;

    public void fetchDayBigDeal() {

        List<Stock> stocks = stockService.findAll();
        if (CollectionUtils.isEmpty(stocks)) return;
        Date now = new Date();
        for (Stock stock : stocks) {
            //判断今天有没有记录过
            List<BigDeal> bigDealList = bigDealRepository.findByRecordDateAndStockId(now, stock.getId());
            if (!CollectionUtils.isEmpty(bigDealList)) continue;

            Map<String, String> params = new HashMap<>();
            params.put("licence", ig507Config.getLicence());
            params.put("dm", stock.getDm());

            List<BigDeal> bigDeals = null;
            try {
                bigDeals = sendReqReturnList(ServiceType.BIGDEAL_FETCH, params, BigDeal.class);
            } catch (Exception e) {
                log.error("获取当天大单逐笔交易失败,", e.getMessage());
            }

            ThreadUtils.sleepSeconds(3);

            if (CollectionUtils.isEmpty(bigDeals)) {
                log.info("stockName:{},stockDm:{}: 今天没有大单交易逐笔信息", stock.getMc(), stock.getDm());
                continue;
            }

            for (BigDeal bigDeal : bigDeals) {
                BigDeal bigDealDb = new BigDeal();
                BeanUtils.copyProperties(bigDeal, bigDealDb);
                bigDealDb.setStockDm(stock.getDm());
                bigDealDb.setStockId(stock.getId());
                bigDealDb.setStockName(stock.getMc());
                bigDealDb.setRecordDate(now);
                bigDealRepository.save(bigDealDb);
            }

        }

    }
}
